﻿using System;

namespace MikeKappel.Com.CS
{
    public static partial class SortingAlgorithms
    {
        public delegate Boolean ShellDelegate(Int32 currentIndex, Int32 currentValue);

        public static void ShellSort(ShellDelegate onSort, Int32[] items)
        {
            Int32 inc;
              for(inc=1;inc<items.Length;inc=3*inc+1);
              for(;inc>0;inc/=3)
              {
                  for(int i=inc;i<items.Length;i+=1)//ceci car le tableau commence par 0
                {
                  int t=items[i];
                  int j=i;
                  while((j>=inc)&&(items[j-inc]>t))
                  {
                    items[j]=items[j-inc];
                    j-=inc;
                    if (onSort != null && !onSort(i, items[i]))
                        goto escape;
                  }
                  items[j]=t;
                }
              }
           escape:
            return;
        }
        public static void ShellSort(Int32[] items)
        {
            ShellSort(null, items);
        }
    }
}
